<?php
App::uses('AppController', 'Controller');
/**
 * UserAddresses Controller
 *
 * @property UserAddress $UserAddress
 * @property PaginatorComponent $Paginator
 */
class UserAddressesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

	public function beforeFilter(){
		$this->Auth->allow('add');
		parent::beforeFilter();
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->UserAddress->recursive = 0;
		$this->Paginator->settings = array(
				'conditions'=>	array('UserAddress.users_id'=>$this->Auth->user('id'))
		);
		$this->set('userAddresses', $this->Paginator->paginate());
		$sysProvinces = $this->UserAddress->SysProvince->find('list',array(
			'conditions'=>array(
				'SysProvince.parent_id'=>139
			)
		));
		$sysCity = $this->UserAddress->SysCity->find('list',array(
				'conditions'=>array(
						'SysCity.parent_id'=>784
				)
		));
		$this->set('sysProvinces',$sysProvinces);
		$this->set('sysCitys',$sysCity);
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->UserAddress->exists($id)) {
			throw new NotFoundException(__('Invalid user address'));
		}
		$options = array('conditions' => array('UserAddress.' . $this->UserAddress->primaryKey => $id));
		$this->set('userAddress', $this->UserAddress->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->UserAddress->create();
			$user_id =$this->Auth->user('id'); 
			if(!$user_id){
				$user_id = 1;		
			}
			$this->request->data['UserAddress']['users_id'] = $user_id;
			if ($this->UserAddress->save($this->request->data)) {
				$this->set('state',true);
				$this->set('msg','操作成功');
				$data = $this->UserAddress->read();
				if(!$this->Auth->user('id')){
					$this->Session->write('noaddress',array($data));
				}
				if($this->request->data['UserAddress']['type'] == 'order'){
					$this->set('sessionAddress',true);
				}else{
					$this->set('sessionAddress',false);
				}
				$this->set('ua',$data);
				$this->set('data',$data);
			} else {
				$this->set('state',false);
				$this->set('msg','操作失败');
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->UserAddress->exists($id)) {
			throw new NotFoundException(__('Invalid user address'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->UserAddress->save($this->request->data)) {
				$this->Session->setFlash(__('The user address has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user address could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('UserAddress.' . $this->UserAddress->primaryKey => $id));
			$this->request->data = $this->UserAddress->find('first', $options);
		}
		$users = $this->UserAddress->User->find('list');
		$sysProvinces = $this->UserAddress->SysProvince->find('list');
		$sysCities = $this->UserAddress->SysCity->find('list');
		$sysAreas = $this->UserAddress->SysArea->find('list');
		$this->set(compact('users', 'sysProvinces', 'sysCities', 'sysAreas'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete() {
		$id = $this->request->data['id'];
		$this->UserAddress->id = $id;
		$state =false;
		if (!$this->UserAddress->exists()) {
			$msg = '地址已删除';
		}else{
			$this->request->onlyAllow('post', 'delete');
			if ($this->UserAddress->delete()) {
				$msg = '操作成功';
				$state = true;
			} else {
				$msg = '操作失败';
			}
		}
		$this->set('state',$state);
		$this->set('msg',$msg);
		$this->set('data',$id);
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->UserAddress->recursive = 0;
		$this->set('userAddresses', $this->Paginator->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->UserAddress->exists($id)) {
			throw new NotFoundException(__('Invalid user address'));
		}
		$options = array('conditions' => array('UserAddress.' . $this->UserAddress->primaryKey => $id));
		$this->set('userAddress', $this->UserAddress->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->UserAddress->create();
			if ($this->UserAddress->save($this->request->data)) {
				$this->Session->setFlash(__('The user address has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user address could not be saved. Please, try again.'));
			}
		}
		$users = $this->UserAddress->User->find('list');
		$sysProvinces = $this->UserAddress->SysProvince->find('list');
		$sysCities = $this->UserAddress->SysCity->find('list');
		$sysAreas = $this->UserAddress->SysArea->find('list');
		$this->set(compact('users', 'sysProvinces', 'sysCities', 'sysAreas'));
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->UserAddress->exists($id)) {
			throw new NotFoundException(__('Invalid user address'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->UserAddress->save($this->request->data)) {
				$this->Session->setFlash(__('The user address has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user address could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('UserAddress.' . $this->UserAddress->primaryKey => $id));
			$this->request->data = $this->UserAddress->find('first', $options);
		}
		$users = $this->UserAddress->User->find('list');
		$sysProvinces = $this->UserAddress->SysProvince->find('list');
		$sysCities = $this->UserAddress->SysCity->find('list');
		$sysAreas = $this->UserAddress->SysArea->find('list');
		$this->set(compact('users', 'sysProvinces', 'sysCities', 'sysAreas'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->UserAddress->id = $id;
		if (!$this->UserAddress->exists()) {
			throw new NotFoundException(__('Invalid user address'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->UserAddress->delete()) {
			$this->Session->setFlash(__('The user address has been deleted.'));
		} else {
			$this->Session->setFlash(__('The user address could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
